<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );
jimport( 'joomla.filesystem.file' );

class jzmluvyViewPartner extends Jview
{
	function display($tpl = null)
  {
    global $mainframe, $option;
    
		$db				=& JFactory::getDBO();
		$document 	= & JFactory::getDocument();
		$user 		= & JFactory::getUser();
    $document->addStyleDeclaration('p {margin: 2px 0px;}');

    $nastavenie = & jzmluvyHelper::nastavenie();
    
		$context = 'com_jzmluvypartnerlist.';
		$params	= &JComponentHelper::getParams('com_jzmluvy');

    $order = ($params->get('razeni_kategorie', 'title-asc') == 'ordering')?'ordering':'title';
    $order_dir = ($params->get('razeni_kategorie', 'title-asc') == 'title-desc')?'DESC':'ASC';
    
    $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );
    
    $filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		'title',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'ASC',			'word' );
     
    $search				= $mainframe->getUserStateFromRequest( $context.'search',			'search', 			'',			'string' );
         
    $filter_kategorie		= $mainframe->getUserStateFromRequest( $context.'filter_kategorie',		'filter_kategorie', 		'',			'string' );
		$filter_archiv		= $mainframe->getUserStateFromRequest( $context.'filter_archiv',		'filter_archiv', 	'',			'string' );
		$filter_anotace		= $mainframe->getUserStateFromRequest( $context.'filter_anotace',		'filter_anotace', 	'1',			'string' );    

    // triedenie
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
    
		// nahratie submenu
    require (JPATH_COMPONENT_ADMINISTRATOR.DS.'admin.submenu.php');
    		
    if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}

		$search = JString::strtolower($search);
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
    
		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$where[] = 'title LIKE '.$searchEscaped.' OR meno LIKE '.$searchEscaped;
		}
		
    $where[] = 'parent_id = 0';
    $where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );
    
		$query = 'SELECT COUNT(id)'
		. ' FROM #__jzmluvy_partner'
		. $where
    ;

		$db->setQuery( $query );
		$total = $db->loadResult();
    
    jimport('joomla.html.pagination');
		$pagination = new JPagination( $total, $limitstart, $limit );

		$query = 'SELECT *'
			. ' FROM #__jzmluvy_partner'
		  . $where
      . $orderby
		;
    
		$db->setQuery( $query, $pagination->limitstart, $pagination->limit );
		$rows = $db->loadObjectList();
		
		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
    
    $lists['search']= $search;
		
		$this->assignRef('items',		$rows);
		$this->assignRef('lists',		$lists);
		$this->assignRef('pagination',	$pagination);
    $this->assignRef('params', $params);
    $this->assignRef('nastavenie', $nastavenie);    

		parent::display($tpl);

	}
    // zistovanie poctu partnerov
    function partner( $dodavatel, $typ ) {
    $db				=& JFactory::getDBO();
		
		$query = 'SELECT COUNT(id)'
		  . ' FROM #__jzmluvy'
		  . ' WHERE dodavatel = '.$dodavatel.' AND catid = '.$typ
		;

		$db->setQuery( $query );
		$total_akt = $db->loadResult();
		    	
		return $total_akt ;  
  }
  
  //*** zistenie poctu partnerov TYP
  function pocet($typ) {
    $db				=& JFactory::getDBO();
		
		$query = 'SELECT COUNT(id)'
		  . ' FROM #__jzmluvy_partner'
		  . ' WHERE typ = '.$typ
		;

		$db->setQuery( $query );
		$total_akt = $db->loadResult();
		    	
		return $total_akt ;  
  }  
  
}
?>